package com.powernode.sqlinjection;

import com.powernode.commons.util.DbUtils;

import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@SuppressWarnings("all")
/**
 * @Author Arrebol
 * @Date 2024/8/2 17:17
 * @Project jdbc
 * @Description：
 * 使用PreparedStatement在数据库表中查询一张图片
 */
public class JdbcTestBlob2 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs=null;
        try {
            conn = DbUtils.getConnection();
            String querySql = "select img from t_img where name = ?";
            ps = conn.prepareStatement(querySql);
            ps.setString(1, "狗狗幼崽");
            rs = ps.executeQuery();
            if(rs.next()){
                //图片存储在结果集中
                InputStream is = rs.getBinaryStream("img");
                OutputStream os=new FileOutputStream(new File("F:\\dog.jpg"));
                int b=0;
                while((b=is.read())!=-1){
                    os.write(b);
                }
                os.flush();
                is.close();
                os.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.close(rs, ps, conn);
        }
    }
}
